#include "led_device.h"
#include "stdio.h"

// led的numdata
// 0    1   2   3       4
// num   r   g   b   bright
#define GET_LED_NUM(x)    (int)((x)->num_data[0])
#define GET_LED_RED(x)    (int)((x)->num_data[1])
#define GET_LED_GREEN(x)  (int)((x)->num_data[2])
#define GET_LED_BLUE(x)   (int)((x)->num_data[3])
#define GET_LED_BRIGHT(x) (int)((x)->num_data[4])

#define SET_LED_NUM(x, v)    (int)((x)->num_data[0] = v)
#define SET_LED_RED(x, v)    (int)((x)->num_data[1] = v)
#define SET_LED_GREEN(x, v)  (int)((x)->num_data[2] = v)
#define SET_LED_BLUE(x, v)   (int)((x)->num_data[3] = v)
#define SET_LED_BRIGHT(x, v) (int)((x)->num_data[4] = v)
#define SET_LED_ALL(x, r, g, b, bright) \
  SET_LED_RED(x, r);                    \
  SET_LED_GREEN(x, g);                  \
  SET_LED_BLUE(x, b);                   \
  SET_LED_BRIGHT(x, bright)

void set_all_led_color(void* device, int red, int green, int blue)
{
  user_device_t dev = (user_device_t)device;
  Adafruit_NeoPixel* pixels = (Adafruit_NeoPixel*)dev->hard_drv;
  SET_LED_ALL(dev, red, green, blue, GET_LED_BRIGHT(dev));
  for (int i = 0; i < GET_LED_NUM(dev); ++i)
  {
    pixels->setPixelColor(i, red, green, blue, GET_LED_BRIGHT(dev));
  }
}

void set_all_led_brightness(void* device, int brightness)
{

  user_device_t dev = (user_device_t)device;
  Adafruit_NeoPixel* pixels = (Adafruit_NeoPixel*)dev->hard_drv;
  SET_LED_BRIGHT(dev, brightness);
  for (int i = 0; i < GET_LED_NUM(dev); i++)
  {
    pixels->setBrightness(GET_LED_BRIGHT(dev));
  }
}

static e_device_err cc_led_read_cb(void* user_data, const char* msg, int type,
                                   void* data)
{
  cc_log("into\r\n");
  return DEVICE_EOK;
}
static e_device_err cc_led_write_cb(void* user_data, const char* msg, int type,
                                    void* data)
{
  cc_log("into\r\n");
  return DEVICE_EOK;
}
static e_device_err cc_led_control_cb(void* user_data, const char* msg, int type,
                                      void* data)
{
  cc_log("into\r\n");
  user_device_t dev = (user_device_t)user_data;
  Adafruit_NeoPixel* pixels = (Adafruit_NeoPixel*)dev->hard_drv;
  SET_LED_BRIGHT(dev, 125);
  if (type == LED_CONTROL_BRIGHT)
  {
    set_all_led_brightness(dev, atoi(msg));
    pixels->show();
  }
  if (type == LED_CONTROL_RED)
  {
    set_all_led_color(dev, 255, 0, 0);
    pixels->show();
  }
  if (type == LED_CONTROL_GREEN)
  {
    set_all_led_color(dev, 0, 255, 0);
    pixels->show();
  }
  if (type == LED_CONTROL_BLUE)
  {
    set_all_led_color(dev, 0, 0, 255);
    pixels->show();
  }
  if (type == LED_CONTROL_WHITE)
  {
    set_all_led_color(dev, 255, 255, 255);
    pixels->show();
  }
  return DEVICE_EOK;
}

e_device_err cc_led_init(void* device)
{
  user_device_t dev = (user_device_t)device;
  cc_log("cb:%p\r\n", dev);
  dev->hard_drv = new Adafruit_NeoPixel(GET_LED_NUM(dev), dev->pin[0],
                                        NEO_GRB + NEO_KHZ800);
  Adafruit_NeoPixel* pixels = (Adafruit_NeoPixel*)dev->hard_drv;
  pixels->begin();
  dev->control_cb = cc_led_control_cb;
  dev->read_cb = cc_led_read_cb;
  dev->write_cb = cc_led_write_cb;
  cc_log("success:%p\r\n", dev);
  return DEVICE_EOK;
}
CC_DEVICE_INIT_MOUNT(cc_led_init, CC_DEVICE_TYPE_LED)
